package org.budo.weixin.work.api.entity;

import java.io.Serializable;
import java.util.Date;

import org.budo.support.lang.util.StringUtil;
import org.budo.time.Time;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
 * @author lmw
 */
@Getter
@Setter
@Accessors(chain = true)
@ToString
@NoArgsConstructor
public class WorkWeixinToken implements Serializable {
    private static final long serialVersionUID = 6520798403939047015L;

    private String accessToken;

    private Integer expiresIn;

    private Date createdAt;

    public WorkWeixinToken(String accessToken, Integer expiresIn) {
        this.setAccessToken(accessToken);
        this.setExpiresIn(expiresIn);

        this.setCreatedAt(new Date());
    }

    public Boolean isOk() {
        return StringUtil.isNotNullOrEmpty(this.getAccessToken()) //  
                && null != this.expiresIn && null != this.createdAt // 
                && Time.when(this.getCreatedAt()).plusSecond(this.getExpiresIn()).isAfter(Time.now().plusMinute(-5)); // 未过期
    }
}